///**
// * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
// * <p>
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// * http://www.apache.org/licenses/LICENSE-2.0
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package cn.xspace.wnx.activity;
//
//import android.content.BroadcastReceiver;
//import android.content.Context;
//import android.content.DialogInterface;
//import android.content.DialogInterface.OnCancelListener;
//import android.content.Intent;
//import android.content.IntentFilter;
//import android.graphics.Bitmap;
//import android.graphics.Point;
//import android.graphics.Rect;
//import android.os.Bundle;
//import android.util.Log;
//import android.view.View;
//import android.widget.ImageView;
//import android.widget.Toast;
//
//import com.baidu.location.BDLocation;
//import com.baidu.location.BDLocationListener;
//import com.baidu.location.LocationClient;
//import com.baidu.location.LocationClientOption;
//import com.baidu.mapapi.SDKInitializer;
//import com.baidu.mapapi.map.BaiduMap;
//import com.baidu.mapapi.map.BaiduMapOptions;
//import com.baidu.mapapi.map.BitmapDescriptorFactory;
//import com.baidu.mapapi.map.MapStatus;
//import com.baidu.mapapi.map.MapStatusUpdate;
//import com.baidu.mapapi.map.MapStatusUpdateFactory;
//import com.baidu.mapapi.map.MapView;
//import com.baidu.mapapi.map.MarkerOptions;
//import com.baidu.mapapi.map.MyLocationConfiguration;
//import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
//import com.baidu.mapapi.map.OverlayOptions;
//import com.baidu.mapapi.model.LatLng;
//import com.baidu.mapapi.utils.CoordinateConverter;
//import com.xspace.android.imcommon.utils.AppUtil;
//import com.xspace.android.xafcommon.base.BaseActivity;
//import com.xspace.android.xafcommon.util.AppConstant;
//import com.xspace.android.xafcommon.util.LogUtil;
//import com.xspace.android.xafcommon.util.T;
//
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//
//import cn.xspace.app.view.TitleBar;
//import cn.xspace.library.util.BldNavigation;
//import cn.xspace.wnx.R;
//
//
//public class BaiduMapActivity extends BaseActivity {
//
//    private final static String TAG = BaiduMapActivity.class.getSimpleName();
//
//    private TitleBar mTitlebar;
//    LocationClient mLocClient;
//    public MyLocationListenner myListener = new MyLocationListenner();
//
//    static BDLocation lastLocation = null;
//    private ImageView locImg;
//
//
//    MapView mMapView;
//    BaiduMap mBaiduMap;
//
//    String address = "";
//    double latitude = 0.0;
//    double longtitude = 0.0;
//    private String locaPath = "";
//    private Point devicePoint;
//
//    private BaiduSDKReceiver mBaiduReceiver;
//    private float mapZoom = 17.0f;
//
//    public class BaiduSDKReceiver extends BroadcastReceiver {
//        public void onReceive(Context context, Intent intent) {
//            String s = intent.getAction();
//            String st1 = "网络出错";
//            if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
//                String st2 = "Key出错";
//                Toast.makeText(BaiduMapActivity.this, st2, Toast.LENGTH_SHORT).show();
//            } else if (s.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
//                Toast.makeText(BaiduMapActivity.this, st1, Toast.LENGTH_SHORT).show();
//            }
//        }
//    }
//
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        AppUtil.createDirs();
//    }
//
//    @Override
//    protected void onResume() {
//        mMapView.onResume();
//        if (mLocClient != null) {
//            mLocClient.start();
//        }
//        super.onResume();
//    }
//
//    @Override
//    protected void onPause() {
//        mMapView.onPause();
//        if (mLocClient != null) {
//            mLocClient.stop();
//        }
//        super.onPause();
//        lastLocation = null;
//    }
//
//    @Override
//    protected void onDestroy() {
//        if (mLocClient != null)
//            mLocClient.stop();
//        mMapView.onDestroy();
//        unregisterReceiver(mBaiduReceiver);
//        super.onDestroy();
//    }
//
//    @Override
//    protected void getIntentData() {
//        super.getIntentData();
//        latitude = getIntent().getDoubleExtra(BldNavigation.LATITUDE, 0);
//        longtitude = getIntent().getDoubleExtra(BldNavigation.LONGITUDE, 0);
//        address = getIntent().getStringExtra(BldNavigation.ADDRESS);
//    }
//
//    @Override
//    protected void initView() {
//        super.initView();
//        setContentView(R.layout.activity_map);
//    }
//
//    @Override
//    protected void initFindViewById() {
//        super.initFindViewById();
//        mMapView = (MapView) findViewById(R.id.bmapView);
//        mTitlebar = (TitleBar) findViewById(R.id.title_bar);
//        locImg = (ImageView) findViewById(R.id.ic_loc);
//    }
//
//    @Override
//    protected void initData() {
//        super.initData();
//        LocationMode mCurrentMode = LocationMode.NORMAL;
//        // 地图初始化
//        mBaiduMap = mMapView.getMap();
//        // 设置缩放等级,默认12
//        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
//        mBaiduMap.setMapStatus(msu);
//        mBaiduMap.setMyLocationEnabled(true);
//        mMapView.setLongClickable(true);
//
//        if (latitude == 0) {
//            mMapView = new MapView(this, new BaiduMapOptions());
//            mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, null));
//            showMapWithLocationClient();
//        } else {
//            LatLng lla = new LatLng(latitude, longtitude);
//            mMapView = new MapView(this, new BaiduMapOptions().mapStatus(new MapStatus.Builder().target(lla).build()));
//            showMap(lla, address);
//        }
//        IntentFilter iFilter = new IntentFilter();
//        iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
//        iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
//        mBaiduReceiver = new BaiduSDKReceiver();
//        registerReceiver(mBaiduReceiver, iFilter);
//
//        /**
//         * 地图状态发生变化的监听
//         */
//        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
//            @Override
//            public void onMapStatusChangeStart(MapStatus mapStatus) {
//                LogUtil.d("onMapStatusChangeStart>>");
//                LogUtil.d("mapStatus.targetScreen>>" + mapStatus.targetScreen.x + ">>>" + mapStatus.targetScreen.y);
//
//            }
//
//            @Override
//            public void onMapStatusChange(MapStatus mapStatus) {
//                LogUtil.d("onMapStatusChange>>");
//                LogUtil.d("mapStatus.targetScreen>>" + mapStatus.targetScreen.x + ">>>" + mapStatus.targetScreen.y);
//            }
//
//            @Override
//            public void onMapStatusChangeFinish(MapStatus mapStatus) {
//                mapZoom = mapStatus.zoom;
//                LogUtil.d("onMapStatusChangeFinish>>");
//                LogUtil.e("mapStatus.targetScreen>>" + mapStatus.targetScreen.x + ">>>" + mapStatus.targetScreen.y);
//                Point tempPoint = mapStatus.targetScreen;
//                if (!tempPoint.equals(devicePoint)) {
//                    devicePoint = tempPoint;
//                }
//            }
//        });
//
//        /**
//         * 设置地图渲染完成的回调
//         */
//        mBaiduMap.setOnMapRenderCallbadk(new BaiduMap.OnMapRenderCallback() {
//            @Override
//            public void onMapRenderFinished() {
//                LogUtil.d("onMapRenderFinished>>");
//            }
//        });
//        /**
//         * 不知道干啥用
//         */
//        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
//            @Override
//            public void onMapLoaded() {
//                LogUtil.d("onMapLoaded");
////                if (lastLocation == null)
////                    return;
////                devicePoint = mBaiduMap.getProjection().toScreenLocation(new LatLng(lastLocation.getLatitude(),
////                        lastLocation.getLatitude()));
////                LogUtil.e("wubaojie>>" + "onMapLoaded : devicePoint  x " + devicePoint.x + " y " + devicePoint.y);
//                Point tempPoint = mBaiduMap.getMapStatus().targetScreen;
//                if (tempPoint != null && tempPoint.x > 0 && tempPoint.y > 0) {
//                    devicePoint = tempPoint;
//                    LogUtil.e("mBaiduMap.getMapStatus().targetScreen>>" + devicePoint.x + ">>>" + devicePoint.y);
//                }
//            }
//        });
//    }
//
//    @Override
//    protected void initEvent() {
//        super.initEvent();
////        if (longtitude != 0) {
////            mTitleBar.setRightLayoutTextviewVisibility(View.GONE);
////        }
//        mTitlebar.setTitle(getResources().getString(R.string.location));
//        mTitlebar.showRightText();
//        mTitlebar.setRightLayoutTextView(getResources().getString(R.string.send));
//        mTitlebar.setRightLayoutClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                shotScope();
//            }
//        });
//        mTitlebar.setLeftLayoutClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
//        locImg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                relocation();
//            }
//        });
//    }
//
//    private void relocation() {
//        if (mBaiduMap != null) {
//            LatLng lla;
//            if (lastLocation != null) {
//                lla = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
//            } else {
//                lla = new LatLng(latitude, longtitude);
//            }
//            CoordinateConverter converter = new CoordinateConverter();
//            converter.coord(lla);
//            converter.from(CoordinateConverter.CoordType.COMMON);
//            LatLng convertLatLng = converter.convert();
//            OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory
//                    .fromResource(R.drawable.icon_map_location_a))
//                    .zIndex(4).draggable(true);
//            mBaiduMap.addOverlay(ooA);
//            MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, mapZoom);
//            mBaiduMap.animateMapStatus(u);
//
//        }
//
//    }
//
//    /**
//     * 截图的方法
//     */
//    private void shotScope() {
//        try {
//            if (devicePoint == null) {
//                T.showSingleL("获取不到定位信息");
//                return;
//            }
//            LogUtil.d("shotScope>" + devicePoint.x + ">>>" + devicePoint.y);
//            Rect rect = new Rect(devicePoint.x, devicePoint.y, devicePoint.x + 500, devicePoint.y + 200);
//            rect.offset(-250, -100);
//            mBaiduMap.snapshotScope(rect, new BaiduMap.SnapshotReadyCallback() {
//                @Override
//                public void onSnapshotReady(Bitmap snapshot) {
//                    locaPath = AppConstant.IMAGE_ADDRESS + System.currentTimeMillis() + AppConstant
//                            .SUFFIX_PNG;
//                    File localFile = new File(locaPath);
//                    FileOutputStream out;
//                    try {
//                        out = new FileOutputStream(localFile);
//                        if (snapshot.compress(Bitmap.CompressFormat.PNG, 100, out)) {
//                            out.flush();
//                            out.close();
//                        }
//                        Toast.makeText(mContext, "屏幕截图成功，图片存在: " + localFile.toString(),
//                                Toast.LENGTH_SHORT).show();
//                        sendLocation();
//                    } catch (FileNotFoundException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    private void showMap(LatLng llA, String address) {
//        mTitlebar.setRightLayoutVisibility(View.GONE);
//        mTitlebar.setTitle("位置");
//        CoordinateConverter converter = new CoordinateConverter();
//        converter.coord(llA);
//        converter.from(CoordinateConverter.CoordType.COMMON);
//        LatLng convertLatLng = converter.convert();
//        OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory
//                .fromResource(R.drawable.icon_map_location_a))
//                .zIndex(4).draggable(true);
//        mBaiduMap.addOverlay(ooA);
//        MapStatusUpdate mapStatusUpdatePoint = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 15.0f);
//        mBaiduMap.animateMapStatus(mapStatusUpdatePoint);
//    }
//
//    private void showMapWithLocationClient() {
//        /**
//         * 等待对话框
//         */
//        String str1 = getResources().getString(R.string.Making_sure_your_location);
//        mProgress.setCanceledOnTouchOutside(false);
//        mProgress.setMessage(str1);
//        mProgress.setOnCancelListener(new OnCancelListener() {
//
//            public void onCancel(DialogInterface arg0) {
//                if (mProgress.isShowing()) {
//                    mProgress.dismiss();
//                }
//                Log.d("map", "cancel retrieve location");
//                finish();
//            }
//        });
//        mProgress.show();
//
//        mLocClient = new LocationClient(this);
//        mLocClient.registerLocationListener(myListener);
//        LocationClientOption option = new LocationClientOption();
//        option.setOpenGps(true);// open gps
//        // option.setCoorType("bd09ll");
//        // Johnson change to use gcj02 coordination. chinese national standard
//        // so need to conver to bd09 everytime when draw on baidu map
//        option.setCoorType("gcj02");
//        option.setScanSpan(30000);
//        option.setAddrType("all");
//        mLocClient.setLocOption(option);
//    }
//
//    /**
//     * 定位SDK监听函数
//     */
//    public class MyLocationListenner implements BDLocationListener {
//        @Override
//        public void onReceiveLocation(BDLocation location) {
//            if (location == null) {
//                return;
//            }
//            LogUtil.d(TAG, "wubaojie>>>" + "onReceiveLocation: " + "addr:" + location.getAddrStr());
//            if (mProgress != null && mProgress.isShowing()) {
//                mProgress.dismiss();
//            }
//            if (lastLocation != null) {
//                if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location
//                        .getLongitude()) {
//                    Log.d("map", "same location, skip refresh");
//                    LogUtil.e(TAG, "wubaojie>>>" + "onReceiveLocation: getLatitude : " + location.getLatitude() +
//                            "    getLongitude : " + location.getLongitude());
//                    // mMapView.refresh(); //need this refresh?
//                    return;
//                }
//            }
//            lastLocation = location;
//            LogUtil.e(TAG, "wubaojie>>>" + "onReceiveLocation: getLatitude : " + location.getLatitude() +
//                    "    getLongitude : " + location.getLongitude());
//            mBaiduMap.clear();
//            LatLng llA = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
//            CoordinateConverter converter = new CoordinateConverter();
//            converter.coord(llA);
//            converter.from(CoordinateConverter.CoordType.COMMON);
//            LatLng convertLatLng = converter.convert();
//            OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory
//                    .fromResource(R.drawable.icon_map_location_a))
//                    .zIndex(4).draggable(true);
//            mBaiduMap.addOverlay(ooA);
//            MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 17.0f);
//            mBaiduMap.animateMapStatus(u);
//        }
//
//        public void onReceivePoi(BDLocation poiLocation) {
//            if (poiLocation == null) {
//                return;
//            }
//        }
//    }
//
//    public void sendLocation() {
//        Intent intent = this.getIntent();
//        intent.putExtra(BldNavigation.LATITUDE, lastLocation.getLatitude());
//        intent.putExtra(BldNavigation.LONGITUDE, lastLocation.getLongitude());
//        intent.putExtra(BldNavigation.ADDRESS, lastLocation.getAddrStr());
//        intent.putExtra(BldNavigation.SNAP_PATH, locaPath);
//        this.setResult(RESULT_OK, intent);
//        finish();
////        overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
//    }
//
//}
